<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/funcs.php';

$error = NULL;
$user = $_POST['user'] ?? null;
if($user) {
	$get_user = $DB->get(
        'admin_user',
        ['id','groupid','status'],
        ['username' => $user, 'pass' => md5(md5($_POST['pass']))]
    );
	if(!isset($get_user['id'])) {
		$error = '错误的用户名或密码';
	} else if($get_user['status'] != 1) {
	    $error = '账户状态异常,请联系管理员';
    } else {
	    $now = time();
	    $userip = ip2long(getIP());
        $DB->update(
            'user',
		    [
		        'login_times[+]' => 1,
                'last_login' => $now,
                'last_ip' => $userip,
            ],
		    ['id' => $get_user['id']]
        );
		$_SESSION['admin_id'] = $get_user['id'];
		$_SESSION['admin_group'] = $get_user['groupid'];
		operate_log('login', 'login');
        goExit('index.php');
    }
}

?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>管理后台</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	<!-- Bootstrap 3.3.7 -->
	<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
	<!-- Font Awesome -->
	<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
	<!-- Ionicons -->
	<link rel="stylesheet" href="bower_components/Ionicons/css/ionicons.min.css">
	<!-- Theme style -->
	<link rel="stylesheet" href="dist/css/AdminLTE.min.css">
	<!-- iCheck -->
	<link rel="stylesheet" href="plugins/iCheck/square/blue.css">
</head>
<body class="hold-transition login-page">
<div class="login-box">
	<div class="login-logo">
		管理后台
	</div>
    <?php if($error) : ?>
    <div class="alert alert-warning alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <?php echo $error; ?>
    </div>
    <?php endif ?>
	<!-- /.login-logo -->
	<div class="login-box-body">
	<p class="login-box-msg">请登录</p>

	<form action="login.php" method="post">
		<div class="form-group has-feedback">
		<input type="text" name="user" class="form-control input-lg" placeholder="">
		<span class="glyphicon glyphicon-user form-control-feedback"></span>
		</div>
		<div class="form-group has-feedback">
		<input type="password" name="pass" class="form-control input-lg" placeholder="">
		<span class="glyphicon glyphicon-lock form-control-feedback"></span>
		</div>
		<div class="row">
			<div class="col-xs-6">
				<div class="checkbox icheck">
					<label>
						<input type="checkbox" name="remember" value="1" /> 记住我
					</label>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
			<!-- /.col -->
				<button type="submit" class="btn btn-primary btn-block btn-flat btn-lg">立刻登录</button>
			</div>
		</div>
	</form>

	</div>
	<!-- /.login-box-body -->
</div>
<!-- /.login-box -->

<!-- jQuery 3 -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="plugins/iCheck/icheck.min.js"></script>
<script>
	$(function () {
	$('input').iCheck({
		checkboxClass: 'icheckbox_square-blue',
		radioClass: 'iradio_square-blue',
		increaseArea: '20%' /* optional */
	});
	});
</script>
</body>
</html>
